﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using ZFine.Code;
using ZFine.Data;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.SystemManage;

namespace Logic.Repository.Project
{
    public class GudaoCarRepository : RepositoryBase<GudaoCarEntity>, IGudaoCarRepository
    {
        /// <summary>
        /// 向股道中添加车辆
        /// </summary>
        /// <param name="addgudaocarlist"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        public int insertGudaoCarList(List<GudaoCarEntity> addgudaocarlist, OperationGudaoCarEntity operation)
        {
            dbcontext.Entry<OperationGudaoCarEntity>(operation).State = EntityState.Added;
            if (operation.OperationPersion != null)
            {
                dbcontext.Entry<UserEntity>(operation.OperationPersion).State = EntityState.Unchanged;
                dbcontext.Entry(operation).Reference(f => f.OperationPersion).CurrentValue = operation.OperationPersion;
            }
            dbcontext.SaveChanges();
            dbcontext.Entry<OperationGudaoCarEntity>(operation).State = EntityState.Unchanged;

            for (int i = 0; i < addgudaocarlist.Count; i++)
            {
                addgudaocarlist[i].Operation = operation;
                if (addgudaocarlist[i].BelongCarriage != null)
                {
                    dbcontext.Entry<CarriageEntity>(addgudaocarlist[i].BelongCarriage).State = EntityState.Added;
                    dbcontext.Entry(addgudaocarlist[i]).Reference(f => f.BelongCarriage).CurrentValue = addgudaocarlist[i].BelongCarriage;
                }
                if (addgudaocarlist[i].Operation != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(addgudaocarlist[i].Operation).State = EntityState.Unchanged;
                    dbcontext.Entry(addgudaocarlist[i]).Reference(f => f.Operation).CurrentValue = addgudaocarlist[i].Operation;
                }
                if (addgudaocarlist[i].BelongPathway != null)
                {
                    dbcontext.Entry<PathwayEntity>(addgudaocarlist[i].BelongPathway).State = EntityState.Unchanged;
                    //dbcontext.Set<GudaoCarEntity>().Attach(addgudaocarlist[i]).BelongPathway = addgudaocarlist[i].BelongPathway;
                    dbcontext.Entry(addgudaocarlist[i]).Reference(f => f.BelongPathway).CurrentValue = addgudaocarlist[i].BelongPathway;
                }
                if (addgudaocarlist[i].PassOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(addgudaocarlist[i].PassOper).State = EntityState.Unchanged;
                    dbcontext.Entry(addgudaocarlist[i]).Reference(f => f.PassOper).CurrentValue = addgudaocarlist[i].PassOper;
                }
                if (addgudaocarlist[i].QiPiaoOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(addgudaocarlist[i].QiPiaoOper).State = EntityState.Unchanged;
                    dbcontext.Entry(addgudaocarlist[i]).Reference(f => f.QiPiaoOper).CurrentValue = addgudaocarlist[i].QiPiaoOper;
                }
                if (addgudaocarlist[i].DuiWeiOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(addgudaocarlist[i].DuiWeiOper).State = EntityState.Unchanged;
                    dbcontext.Entry(addgudaocarlist[i]).Reference(f => f.DuiWeiOper).CurrentValue = addgudaocarlist[i].DuiWeiOper;
                }
                if (addgudaocarlist[i].RecoveryOper != null)
                {
                    dbcontext.Entry<OperationGudaoCarEntity>(addgudaocarlist[i].RecoveryOper).State = EntityState.Unchanged;
                    dbcontext.Entry(addgudaocarlist[i]).Reference(f => f.RecoveryOper).CurrentValue = addgudaocarlist[i].RecoveryOper;
                }
                dbcontext.Entry<GudaoCarEntity>(addgudaocarlist[i]).State = EntityState.Added;
                dbcontext.SaveChanges();
            }
            return 0;
        }

        public List<GudaoCarEntity> FindEntityAsNoTrackinglist(Expression<Func<GudaoCarEntity, bool>> predicate)
        {
            return dbcontext.Set<GudaoCarEntity>().Where(predicate).AsNoTracking().ToList();
        }

       

        public List<GudaoCarEntity> GetIncludeList(Expression<Func<GudaoCarEntity, bool>> predicate)
        {
            return dbcontext.Set<GudaoCarEntity>().Include(f => f.BelongPathway).Include(f => f.BelongCarriage)
                .Include(f => f.Operation).Include(f => f.PassOper).Include(f => f.QiPiaoOper).Include(f=>f.WorkPlanOper).Include(f=>f.ClearGudaoOper).Include(f=>f.BelongPathway).Include(f=>f.BelongCarriage).Include(f=>f.DuiWeiOper).Include(f=>f.RecoveryOper).Where(predicate)
                .OrderBy(f => f.CarGudaoIndex).ToList();
        }

        public List<GudaoCarEntity> GetIncludeListNoTrackinglist(Expression<Func<GudaoCarEntity, bool>> predicate)
        {
            return dbcontext.Set<GudaoCarEntity>().Include(f => f.BelongPathway).Include(f => f.BelongCarriage)
                .Include(f => f.Operation).Include(f => f.PassOper).Include(f => f.QiPiaoOper).Include(f => f.WorkPlanOper).Include(f => f.ClearGudaoOper).Include(f => f.BelongPathway).Include(f => f.BelongCarriage).Include(f => f.QiPiaoOper).Include(f => f.RecoveryOper).Where(predicate)
                .OrderBy(f => f.CarGudaoIndex).AsNoTracking().ToList();
        }

        public List<GudaoCarEntity> FindList(Expression<Func<GudaoCarEntity, bool>> predicate, Pagination pagination)
        {
            bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
            string[] _order = pagination.sidx.Split(',');
            MethodCallExpression resultExp = null;
            var tempData = dbcontext.Set<GudaoCarEntity>().Include(f => f.BelongPathway).Include(f => f.BelongCarriage).Include(f => f.Operation).Include(f => f.PassOper).Include(f => f.QiPiaoOper).Include(f => f.DuiWeiOper).Include(f => f.RecoveryOper).Include(f=>f.RecoveryOper.OperationPersion).Include(f=>f.ClearGudaoOper).Where(predicate);
            foreach (string item in _order)
            {
                string _orderPart = item;
                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
                string[] _orderArry = _orderPart.Split(' ');
                string _orderField = _orderArry[0];
                bool sort = isAsc;
                if (_orderArry.Length == 2)
                {
                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
                }
                var parameter = Expression.Parameter(typeof(GudaoCarEntity), "t");
                var property = typeof(GudaoCarEntity).GetProperty(_orderField);
                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
                var orderByExp = Expression.Lambda(propertyAccess, parameter);
                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(GudaoCarEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
            }
            tempData = tempData.Provider.CreateQuery<GudaoCarEntity>(resultExp);
            pagination.records = tempData.Count();
            tempData = tempData.Skip<GudaoCarEntity>(pagination.rows * (pagination.page - 1)).Take<GudaoCarEntity>(pagination.rows).AsQueryable();
            return tempData.ToList();
        }

        public GudaoCarEntity FindEntity(Expression<Func<GudaoCarEntity, bool>> predicate)
        {
            return dbcontext.Set<GudaoCarEntity>().Include(f => f.BelongPathway).Include(f => f.BelongCarriage)
                .Include(f => f.Operation).Include(f => f.PassOper).Include(f => f.QiPiaoOper).Include(f => f.DuiWeiOper).Include(f => f.RecoveryOper).FirstOrDefault(predicate);
        }

        public int Update(GudaoCarEntity entity)
        {
            dbcontext.Set<GudaoCarEntity>().Attach(entity);
            PropertyInfo[] props = entity.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.PropertyType.FullName.Contains("Entity"))
                {
                    break;
                }
                else if (prop.GetValue(entity, null) != null)
                {
                    if (prop.GetValue(entity, null).ToString() == "&nbsp;")
                        dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
                    dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
                }
            }
            if (entity.BelongCarriage != null)
            {
                dbcontext.Entry<CarriageEntity>(entity.BelongCarriage).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.BelongCarriage).CurrentValue = entity.BelongCarriage;
            }
            if (entity.BelongPathway != null)
            {
                dbcontext.Entry<PathwayEntity>(entity.BelongPathway).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.BelongPathway).CurrentValue = entity.BelongPathway;
            }
            if (entity.Operation != null)
            {
                dbcontext.Entry<OperationGudaoCarEntity>(entity.Operation).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.Operation).CurrentValue = entity.Operation;
            }
            if (entity.PassOper != null)
            {
                dbcontext.Entry<OperationGudaoCarEntity>(entity.PassOper).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.PassOper).CurrentValue = entity.PassOper;
            }
            if (entity.QiPiaoOper != null)
            {
                dbcontext.Entry<OperationGudaoCarEntity>(entity.QiPiaoOper).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.QiPiaoOper).CurrentValue = entity.QiPiaoOper;
            }
            if (entity.DuiWeiOper != null)
            {
                dbcontext.Entry<OperationGudaoCarEntity>(entity.DuiWeiOper).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.DuiWeiOper).CurrentValue = entity.DuiWeiOper;
            }
            if (entity.RecoveryOper != null)
            {
                dbcontext.Entry<OperationGudaoCarEntity>(entity.RecoveryOper).State = EntityState.Unchanged;
                dbcontext.Entry(entity).Reference(f => f.RecoveryOper).CurrentValue = entity.RecoveryOper;
            }
            dbcontext.Entry<GudaoCarEntity>(entity).State = EntityState.Modified;
            return dbcontext.SaveChanges();
        }

        public List<GudaoCarEntity> FindList(Func<GudaoCarEntity, bool> p)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 排车查询记录
        /// </summary>
        /// <param name="strSql"></param>
        /// <returns></returns>
        public List<GudaoCarEntity> GetClearCarList(string strSql)
        {
            return dbcontext.Database.SqlQuery<GudaoCarEntity>(strSql).ToList<GudaoCarEntity>();
        }

    }
}
